from xml.dom import minidom

import sys


class View:
    id: str
    type: str

    def name(self):
        print("id = %s" % self.id)
        if self.id == "":
            return ""
        names = self.id.split("\_")
        name = ""
        for s in names:
            name += s[0].upper() + s[1:]
        return name

    pass


views = list()
elements = list()

path = "/Users/cai/AndroidProjects/sxwPad/UserOrder/src/main/res/layout/pop_mine_center.xml"

f = open(path)
text = f.read()

dom = minidom.parseString(text)
doc: minidom.Element = dom.childNodes[0]

di = dict()


def addView(e: minidom.Element):
    if e.nodeType == minidom.DocumentType.TEXT_NODE:
        return
    view_id = e.getAttribute("android:id").replace("@+id/", "").replace("@id/", "")

    instance = View()

    type_name = e.nodeName
    instance.type = type_name
    instance.id = view_id

    if e.childNodes.length == 0:
        pass
    for c in e.childNodes:
        addView(c)
        elements.append(c)

    di[view_id] = type_name


addView(doc)


def get_name(id):
    if id == "":
        return ""
    names = id.split("_")
    name = "m"
    for s in names:
        name += s[0].upper() + s[1:]
    return name


TAB = "    "
for key in di.keys():
    view_id: str = key
    if len(view_id.replace(" ", "")) == 0:
        pass
    else:
        field_name = get_name(key)
        view_type = di[key]
        print(TAB + "private var %s: %s? = null" % (field_name, view_type))

print(TAB + "private fun findViews(view: View) {")

for key in di.keys():
    view_id: str = key
    if len(view_id.replace(" ", "")) == 0:
        pass
    else:
        field_name = get_name(key)
        view_type = di[key]
        print(TAB * 2 + "%s = view.findViewById(%s) as %s" % (field_name, "cn.sxw365.userorder.R.id." + view_id, view_type))

print(TAB + "}")
